package com.ycy.create;

import com.ycy.BaseTest;
import org.flowable.bpmn.model.*;
import org.flowable.bpmn.model.Process;
import org.flowable.engine.*;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.engine.runtime.ProcessInstanceBuilder;
import org.flowable.task.api.Task;
import org.junit.Test;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author he.ai aihehe123@gmail.com
 * 使用场景：
 * 功能描述：
 */
public class HolidayRequest extends BaseTest {

  @Test
  public void test() {
    // 部署流程
    RepositoryService repositoryService = processEngine.getRepositoryService();
    Deployment deployment = repositoryService.createDeployment()
            .addClasspathResource("bp/holiday-request.bpmn20.xml")
            .deploy();
  }

  @Test
  public void create() {

    Long currentTime = System.currentTimeMillis();
    Map<String, Object> variables = new HashMap<String, Object>();
    variables.put("employee", "employee" + currentTime);
    variables.put("nrOfHolidays", "nrOfHolidays" + currentTime);
    variables.put("description", "description" + currentTime);

    RuntimeService runtimeService = processEngine.getRuntimeService();

    // 创建流程实例 用户后续等待这个状态即可  发起流程
    ProcessInstance processInstance =
            runtimeService.startProcessInstanceByKey("holidayRequest", variables);

    System.out.println(processInstance.getBusinessKey());
    System.out.println(processInstance.getId());
    System.out.println(processInstance.getProcessInstanceId());

    //创建流程实例
    ProcessInstanceBuilder processInstanceBuilder = runtimeService.createProcessInstanceBuilder();
    processInstanceBuilder.processDefinitionKey("holidayRequest")
            .businessKey(currentTime + "")
            .name("tiao")
            .variables(variables);

    ProcessInstance start = processInstanceBuilder.start();
    System.out.println(start.getBusinessKey());
    System.out.println(start.getId());
    System.out.println(start.getProcessInstanceId());
    //1613564903590 : 52511

  }


  @Test
  public void flowelement() {

    List<Task> list = processEngine.getTaskService().createTaskQuery().processInstanceId("27510").list();
    for (Task task : list) {
      System.out.println(task);
    }

  }

  @Test
  public void pass(){
    HashMap<String, Object> variables = new HashMap<>();
    variables.put("approved",true);
    taskService.complete("20017", variables);
  }


  @Test
  public void query(){
    BpmnModel bpmnModel = processEngine.getRepositoryService().getBpmnModel("test_subProcess:1:3");
    Process process = bpmnModel.getMainProcess();
    FlowElement flowElement = process.getFlowElementMap().get("sid-A0075F5A-7ED9-4341-B32B-78B8D14A7587");
    FlowElementsContainer parentContainer = flowElement.getParentContainer();
    if (parentContainer instanceof SubProcess) {
      System.out.println(parentContainer);
    }
  }


}
